package map.areaeffect;

import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import model.ModelObject;
import effects.Effect;
import factory.EffectFactory;

public class AreaEffect extends ModelObject {
	
	private int rate;
	private Effect effect;
	
	public AreaEffect() {
		
		super("None");
		this.rate = 1;
		this.effect = null;
	}
	
	public AreaEffect(Effect effect, int rate, String name) {
		
		super(name);
		this.rate = rate;
		this.effect = effect;
	}
	
	public int getRate() {
		
		return this.rate;
	}
	
	public Effect getEffect() {
		
		return this.effect;
	}
	
	@Override
	public Memento getMemento() {
		return new Memento() {
			
			public LoadSaveObject getState() {
				LoadSaveObjectComposite ret = new LoadSaveObjectComposite( "", "AreaEffect" );
				Iterator<LoadSaveObject> iter = AreaEffect.super.getMemento().getState().getIterator();
				for( iter.init(); iter.hasNext(); iter.next() ) {
					ret.add( iter.getCurrKey().getValue(), iter.getCurrVal());
				}
				
				LoadSaveObject e = effect.getMemento().getState();
				e.setName( "effect" );
				ret.add(e.getName(), e);

				ret.add("rate", new LoadSavePrimitive("rate", "int", String.valueOf(rate)));
				
				return ret;
			}

			public void restoreState(LoadSaveObject lso) {
				AreaEffect.super.getMemento().restoreState( lso );
				
				effect = EffectFactory.createEffect( lso.get("effect") );
				rate = Integer.valueOf(lso.get("rate").getValue());
			}
		};
	}
	
	public String toString() {
		
		return getName();
	}
	
}
